package me.sealer.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.sealer.ssm.constant.ChinaAreaLevel;
import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.constant.PermissionConstant;
import me.sealer.ssm.controller.dto.CommonResponse;
import me.sealer.ssm.model.ChinaArea;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.ChinaAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述： $ ChinaAreaController
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年11月15日 14时42分05秒
 */
@RestController
@Api(tags = "中国行政区域查询相关api")
@RequestMapping("/chinaAreaOperations")
@RequiresPermissions(value = {PermissionConstant.AREA_MGMT})
public class ChinaAreaController {
    private final ChinaAreaService chinaAreaService;

    public ChinaAreaController(ChinaAreaService chinaAreaService) {
        this.chinaAreaService = chinaAreaService;
    }

    @ApiOperation(value = "根据条件查询区域")
    @GetMapping(value = "/queryAreasPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<PageResult<ChinaArea>> queryAreasPage(
            @RequestParam(value = "name", required = false) @ApiParam(value = "区域名称") String name,
            @RequestParam(value = "level", required = false) @ApiParam(value = "区域级别", allowableValues = "0, 1, 2, 3, 4") Integer level,
            @RequestParam(value = "zipCode", required = false) @ApiParam(value = "邮政编码") Integer zipCode,
            @RequestParam(value = "cityCode", required = false) @ApiParam(value = "区号") String cityCode,
            @RequestParam(value = "code", required = false) @ApiParam(value = "行政编号") Long code,
            @RequestParam(value = "parentCode", required = false) @ApiParam(value = "父级行政编码") Long parentCode,
            @RequestParam(value = PageConstant.PAGE_NO) @ApiParam(value = "第几页数据", required = true) Integer pageNo,
            @RequestParam(value = PageConstant.PAGE_SIZE) @ApiParam(value = "每页几条数据", required = true) Integer pageSize) {

        PageResult<ChinaArea> pageResult = chinaAreaService.queryAreasPage(name, level, zipCode, cityCode, code, parentCode, pageNo, pageSize);
        return new CommonResponse<>(pageResult);
    }

    @ApiOperation(value = "中国所有省份查询")
    @GetMapping(value = "/province", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<ChinaArea>> queryProvinces() {

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.PROVINCE, 0);
        return new CommonResponse<>(chinaAreaList);
    }

    @ApiOperation(value = "根据省级编码查询所有城市")
    @GetMapping(value = "/city", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<ChinaArea>> queryCities(@RequestParam("provinceCode") @ApiParam(value = "省份编码") Long provinceCode) {

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.CITY, provinceCode);
        return new CommonResponse<>(chinaAreaList);
    }

    @ApiOperation(value = "根据市级编码查询所有县")
    @GetMapping(value = "/county", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<ChinaArea>> queryCounties(@RequestParam("cityCode") @ApiParam(value = "市级编码") Long cityCode) {

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.COUNTY, cityCode);
        return new CommonResponse<>(chinaAreaList);
    }

    @ApiOperation(value = "根据县级编码查询所有乡镇")
    @GetMapping(value = "/town", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<ChinaArea>> queryTowns(@RequestParam("countyCode") @ApiParam(value = "县级编码") Long countyCode) {

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.TOWN, countyCode);
        return new CommonResponse<>(chinaAreaList);
    }

    @ApiOperation(value = "根据乡镇编码查询所有村级区域")
    @GetMapping(value = "/village", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<ChinaArea>> queryVillages(@RequestParam("townCode") @ApiParam(value = "镇级") Long townCode) {

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.VILLAGE, townCode);
        return new CommonResponse<>(chinaAreaList);
    }
}
